package com.ebelter.myehc.utils;

import android.text.TextUtils;
import android.widget.TextView;

import com.ebelter.btcomlib.models.bluetooth.products.scale.ScaleManager;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.myehc.R;

/**
 * 通用的工具集合
 */

public class EHCCommonUtils {

    public static String unitProcess(float value,boolean isKgModel){
        if (value<0) value=0;
        value= NumUtils.numBaoLiuWei1(value);
        if (isKgModel){
            return value+"";
        }else {
            return NumUtils.numBaoLiuWei1(ScaleManager.kg2lb(value))+"";
        }
    }


    public static int getDisplaySrcRes(float avgValue,float newstValue){

        avgValue = NumUtils.numBaoLiuWei1(avgValue);
        newstValue = NumUtils.numBaoLiuWei1(newstValue);

        if (avgValue==newstValue&&avgValue!=0){
            return R.drawable.ic_hold;
        }else if (avgValue<newstValue){
            return R.drawable.ic_shangshen;
        }else if (avgValue>newstValue){
            return R.drawable.ic_xiajiang;
        }
        return 0;
    }


    public static String m2km(float m,int baoliuwei){

        float km= (float) (m*1.0/1000.0);
        if (baoliuwei==0){
            return NumUtils.numBaoLiuWei0(km)+"";
        }else if (baoliuwei==1){
            return NumUtils.numBaoLiuWei1(km)+"";
        }else if (baoliuwei==2){
            return NumUtils.numBaoLiuWei2(km)+"";
        }
        return km+"";
    }

    public static String m2km2(float m,boolean isdown){
        float km= (float) (m*1.0/1000.0);
        if (isdown){
            km=NumUtils.numBaoLiuWei2_down(km);
        }else {
            km=NumUtils.numBaoLiuWei2(km);
        }

      return NumUtils.floatDis2XSH(km);
    }



    public static void setTextViewStrAutoDisplay(TextView topTv,String dis,int limitNum,int xiao,int narmal){
       if (TextUtils.isEmpty(dis))return;

       if (dis.length()>limitNum){
           ViewUtils.setTextSize(topTv,xiao);
       }else {
           ViewUtils.setTextSize(topTv,narmal);
       }
       ViewUtils.setTextViewStr(topTv,dis);
    }


    public static String algorismToHEXString(int i) {
        String result = "";
        result = Integer.toHexString(i);
        if (result.length() % 2 == 1) {
            result = "0" + result;
        }
        result = result.toUpperCase();
        return result;
    }

    public static int hexStringToAlgorism(String hex) {
        hex = hex.toUpperCase();
        int max = hex.length();
        int result = 0;
        for (int i = max; i > 0; i--) {
            char c = hex.charAt(i - 1);
            int algorism = 0;
            if (c >= '0' && c <= '9') {
                algorism = c - '0';
            } else {
                algorism = c - 55;
            }
            result += Math.pow(16, max - i) * algorism;
        }
        return result;
    }
}
